package org.dromara.business.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.business.domain.Report;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.business.domain.vo.ReportVo;
import org.dromara.business.domain.bo.ReportBo;
import org.dromara.business.service.ReportService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 反馈记录
 *
 * @author zlj
 * @date 2024-08-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/report")
public class ReportController extends BaseController {

    private final ReportService reportService;

    /**
     * 查询反馈记录列表
     */
    @SaCheckPermission("business:report:list")
    @GetMapping("/list")
    public TableDataInfo<ReportVo> list(ReportBo bo, PageQuery pageQuery) {
        return reportService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取反馈记录详细信息
     *
     * @param reportId 主键
     */
    @SaCheckPermission("business:report:query")
    @GetMapping("/{reportId}")
    public R<ReportVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long reportId) {
        return R.ok(reportService.queryById(reportId));
    }

    /**
     * 新增反馈记录
     */
    @SaCheckPermission("business:report:add")
    @Log(title = "反馈记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ReportBo bo) {
        return toAjax(reportService.insertByBo(bo));
    }

    /**
     * 修改反馈记录
     */
    @SaCheckPermission("business:report:edit")
    @Log(title = "反馈记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ReportBo bo) {
        return toAjax(reportService.updateByBo(bo));
    }

    /**
     * 删除反馈记录
     *
     * @param reportIds 主键串
     */
    @SaCheckPermission("business:report:remove")
    @Log(title = "反馈记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{reportIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] reportIds) {
        return toAjax(reportService.deleteWithValidByIds(List.of(reportIds), true));
    }

    /**
     * 反馈通过(处理完成)
     * @param reportId
     */
    @SaCheckPermission("business:report:approve")
    @GetMapping("/approve/{reportId}")
    public R<ReportVo> approve(@NotNull(message = "主键不能为空")
                               @PathVariable Long reportId) {
        Report report = reportService.getById(reportId);
        if (report==null){
            throw new RuntimeException("没有查到相关反馈记录");
        }
        report.setStatus(2);
        reportService.updateById(report);
        return R.ok();
    }


    /**
     * 反馈不通过
     * @param reportId
     */
    @SaCheckPermission("business:report:reject")
    @GetMapping("reject/{reportId}")
    public R<ReportVo> reject(@NotNull(message = "主键不能为空")
                               @PathVariable Long reportId) {
        Report report = reportService.getById(reportId);
        if (report == null) {
            throw new RuntimeException("没有查到相关反馈记录");
        }
        report.setStatus(3);
        reportService.updateById(report);
        return R.ok();
    }

}
